<?php
/*
 * @Author: lokei
 * @Date: 2023-01-13 22:51:00
 * @LastEditors: lokei
 * @LastEditTime: 2023-01-14 18:08:31
 * @Description: 
 */
namespace App\Http\Controllers\Master;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Master\DriverModel;
use Illuminate\Http\Request;

class DriverController extends Controller {

    public function get(Request $request) {
        $driver = DriverModel::where('id', '=', $request->input('id'))->first();
        $res = ResultTool::success();
        $res['data'] = $driver;
        return $res;
    }

    public function list() {
        $items = DriverModel::get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }
    
    public function register(Request $request) {
        $user = app('auth')->user();
        $is_exist = DriverModel::where('uid', '=', $user->uid)->first();
        if(!is_null($is_exist)) {
            $res = ResultTool::fail();
            $res['errorMsg'] = '已注册车主，请不要重复注册';
            return $res;
        }
        $driver = new DriverModel();
        $driver->uid = $user->uid;
        $driver->name = $request->input('name');
        $driver->mobile = $request->input('mobile');
        $driver->id_number = $request->input('id_number');
        $driver->series = $request->input('series');
        $driver->color = $request->input('color');
        $driver->car_number = $request->input('car_number');
        $driver->driving_licence = $request->input('driving_licence');
        $driver->car_licence = $request->input('car_licence');
        $driver->save();
        return ResultTool::success();
    }

    public function update(Request $request) {
        $driver = DriverModel::where('id', '=', $request->input('id'))->first();
        $driver->name = $request->input('name');
        $driver->mobile = $request->input('mobile');
        $driver->id_number = $request->input('id_number');
        $driver->series = $request->input('series');
        $driver->color = $request->input('color');
        $driver->car_number = $request->input('car_number');
        $driver->driving_licence = $request->input('driving_licence');
        $driver->car_licence = $request->input('car_licence');
        $driver->save();
        return ResultTool::success();
    }

    public function accept(Request $request) {
        $driver = DriverModel::where('id', '=', $request->input('id'))->first();
        $driver->status = '1';
        $driver->save();
        return ResultTool::success();
    }

    public function reject(Request $request) {
        $driver = DriverModel::where('id', '=', $request->input('id'))->first();
        $driver->status = '2';
        $driver->save();
        return ResultTool::success();
    }

    public function ofMe() {
        $user = app('auth')->user();
        $driver = DriverModel::where('uid', '=', $user->uid)->first();
        $res = ResultTool::success();
        $res['data'] = $driver;
        return $res;
    }
}